﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAO;
using BUL;
using MySql.Data.MySqlClient;

namespace Hospital
{
    public partial class ManagerPatient : System.Web.UI.Page
    {
        PatientBUL patient = new PatientBUL();
        NextOfKindBUL kind = new NextOfKindBUL();
        ClinicBUL clinic = new ClinicBUL();
        WardBUL ward = new WardBUL();
        DataTable dt = new DataTable();
        PatientDAO pd = new PatientDAO();
        NextOfKindDAO nok = new NextOfKindDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                listGender.Items.Add("Nam");
                listGender.Items.Add("Nữ");
                listType.Items.Add("Ngoại trú");
                listType.Items.Add("Nội trú");
                loadClinic();
            }
        }
        private void loadClinic() {
            dt = clinic.load();
            listClinic.DataSource = dt;
            listClinic.DataTextField = "name";
            listClinic.DataValueField = "idClinic";
            listClinic.DataBind();
            dt.Clear();
        }
        protected void grvPatient_SelectedIndexChanged(object sender, EventArgs e)
        {
            //load benh nhan
            txtCode.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[1].Text);
            txtName.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[2].Text);
            listGender.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[3].Text);
            txtAge.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[4].Text);
            txtPhone.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[5].Text);
            txtAddress.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[6].Text);
            txtMarial.Text =HttpUtility.HtmlDecode(grvPatient.SelectedRow.Cells[7].Text);
            //load than nhan
            dt = patient.loadTN(txtCode.Text);
            if(dt.Rows.Count !=0){
                txtNameTN.Text = dt.Rows[0][0].ToString();
                txtAddressTN.Text = dt.Rows[0][1].ToString();
                txtPhoneTN.Text = dt.Rows[0][2].ToString();
                txtRelation.Text = dt.Rows[0][3].ToString();
            }
            dt.Clear();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Equals("")) {
                //insert TN
                if (!txtNameTN.Text.Equals(""))
                {
                    nok.Fullname = txtNameTN.Text;
                    nok.Address = txtAddressTN.Text;
                    nok.Relationship = txtRelation.Text;
                    nok.Phone = txtPhoneTN.Text;
                    kind.InsertTN(nok);
                }
                //insert BN
                pd.Fullname = txtName.Text;
                pd.Gender = listGender.Text;
                pd.Age = txtAge.Text;
                pd.Address = txtAddress.Text;
                pd.Marital = txtMarial.Text;
                pd.Phone = txtPhone.Text;
                pd.IdNextOfKind = kind.getIdLast(); btnDelete.Text = kind.getIdLast().ToString();
                pd.IdClinic = Int32.Parse(listClinic.SelectedValue.ToString());
                patient.InsertBN(pd);
                reset();
                Page_Load(sender, e);
                listType_SelectedIndexChanged(sender, e);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!txtCode.Text.Equals("")) {
                patient.deleteBN(txtCode.Text);
                Page_Load(sender, e);
                listType_SelectedIndexChanged(sender, e);
                reset();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //Cập nhật benh nhan
            if (!txtCode.Text.Equals(""))
            {
                pd.IdPatient = Int32.Parse(txtCode.Text);
                pd.Fullname = txtName.Text;
                pd.Gender = listGender.Text;
                pd.Age = txtAge.Text;
                pd.Address = txtAddress.Text;
                pd.Marital = txtMarial.Text;
                pd.Phone = txtPhone.Text;
                pd.IdClinic = Int32.Parse(listClinic.SelectedValue.ToString());
                patient.updateBN(pd);
                if (kind.getIdByPatient(txtCode.Text) != 0) {
                    nok.Fullname = txtNameTN.Text;
                    nok.Address = txtAddressTN.Text;
                    nok.Relationship = txtRelation.Text;
                    nok.Phone = txtPhoneTN.Text;
                    nok.IdNextOfKind = kind.getIdByPatient(txtCode.Text);
                    kind.updateTN(nok);
                }
                reset();
                Page_Load(sender, e);
                listType_SelectedIndexChanged(sender, e);
            }
        }

        protected void txtReset_Click(object sender, EventArgs e)
        {
            reset();
        }
        private void reset() { 
            txtCode.Text = "";
            txtName.Text = "";
         //   listGender.Text = "";
            txtAge.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
            txtMarial.Text = "";
            txtNameTN.Text = "";
            txtAddressTN.Text = "";
            txtPhoneTN.Text = "";
            txtRelation.Text = "";
          
        }

        protected void listType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listType.SelectedIndex.ToString().Equals("0"))
            {
                grvPatient.DataSource = patient.loadTableOn();
                grvPatient.DataBind(); 
            }
            else {
                dt = ward.load();
                if (dt.Rows.Count != 0)
                {
                    listPhong.DataSource = dt;
                    listPhong.DataTextField = "name";
                    listPhong.DataValueField = "idWard";
                    listPhong.DataBind();
                    dt.Clear();
                    grvPatient.DataSource = patient.loadTableIn(listPhong.Text);
                    grvPatient.DataBind();
                }
            }
        }
    }
}